// CherryMerry ( Mikhail Tomilenko ), Reverse Entertainment, 2021-2024


#include "RespawnMenu.h"

#include <NifestGame/Core/Public/NF_Character.h>
#include <NifestGame/Core/Public/NF_GameInstance.h>
#include <NifestGame/ActorComponents/Public/PlayerHealthManager.h>

#include <Kismet/GameplayStatics.h>
#include "Components/Button.h"
#include "Components/TextBlock.h"

void URespawnMenu::NativeConstruct()
{
	ACharacter* Character = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);
	UPlayerHealthManager* HealthManager = Character->FindComponentByClass<UPlayerHealthManager>();

	TextBlock_CurrentTotemsAmount->SetText(FText::AsNumber(HealthManager->CurrentTotemsAmount));
	TextBlock_MaxTotemsAmount->SetText(FText::AsNumber(HealthManager->MaxTotemsAmount));

	if (Button_Respawn)
	{
		Button_Respawn->OnClicked.AddDynamic(this, &URespawnMenu::CallRespawn);
	}
	if (Button_LoadGame)
	{
		Button_LoadGame->OnClicked.AddDynamic(this, &URespawnMenu::CallLoadGameMenu);
	}
	if (Button_QuitToMainMenu)
	{
		Button_QuitToMainMenu->OnClicked.AddDynamic(this, &URespawnMenu::QuitToMainMenu);
	}
}
	
void URespawnMenu::CallRespawn()
{
	ACharacter* Character = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);
	UPlayerHealthManager* HealthManager = Character->FindComponentByClass<UPlayerHealthManager>();

	HealthManager->Respawn();
}

void URespawnMenu::CallLoadGameMenu()
{
	if (UNF_GameInstance* GameInstance = Cast<UNF_GameInstance>(UGameplayStatics::GetGameInstance(GetWorld())))
	{
		GameInstance->StartGame(EStartGameType::SGTE_LoadGame);
	}
}

void URespawnMenu::QuitToMainMenu()
{
	UGameplayStatics::OpenLevel(this, "MainMenu");
}
